<?php

class News extends Admin_Controller {
    /**
     * Int Blog categoryID
     * @var type 
     */
    protected $_categoryID = 1;
    
    /**
     * Array that contains the validation rules
     * @access protected
     * @var array
     */
    protected $validation_rules = array(
        'title' => array(
                'field' => 'title',
                'label' => 'Tiêu đề',
                'rules' => 'trim|htmlspecialchars|required|max_length[200]'
        ),
        array(
                'field' => 'keywords',
                'label' => 'Từ khóa',
                'rules' => 'trim'
        ),
        array(
                'field' => 'intro',
                'label' => 'Mô tả bài viết',
                'rules' => 'trim|required'
        ),
        array(
                'field' => 'content',
                'label' => 'Mô tả bài viết',
                'rules' => 'trim|required'
        )
    );
    
    public function __construct() {
        parent::__construct();
        
        //Load blog model
        $this->load->model('backend/blog_m');
    }

    /**
     * Default function
     */
    public function index() {
        
        //Get array blogs
        $arrData = $this->blog_m->get_object_by(array('category_id' => $this->_categoryID));
        
        //Assign to view
        $this->assign('arrData', $arrData);
        
        //Set layout title
        $this->layout_title = 'Quản lý tin tức';
    }

    /**
     * Create new object
     */
    public function create() {
        //Load validation lib
        $this->load->library('form_validation');
        //Set rules
        $this->form_validation->set_rules($this->validation_rules);
        
        //Check is post
        if ($this->input->post())
        {
            if ($this->form_validation->run())
            {
                //Load keyword library
                $this->load->library('keywords');
                
                //Get current user
                $globals = $this->load->get_var('_globals');
                
                $id = $this->blog_m->insert_object(array(
                        'category_id'       => $this->_categoryID,
                        'title'             => $this->input->post('title'),
                        'slug'              => $this->removeUnicode($this->input->post('title')),
                        'thumb'             => $this->input->post('thumbnail_url'),
                        'keywords'          => $this->keywords->process($this->input->post('keywords')),
                        'intro'             => $this->input->post('intro'),
                        'body'              => $this->input->post('content'),
                        'status'            => $this->input->post('status'),
                        'created_on'		=> time(),
                        'updated_on'		=> time(),
                        'comments_enabled'	=> $this->input->post('comment'),
                        'author_id'         => $globals['user']['id'],
                        'type'              => 1 // 1:article
                ));
                
                if ($id)
                {
                        $this->session->set_flashdata('success', sprintf('Đã thêm bài viết "%s".', $this->input->post('title')));
                }
                else
                {
                        $this->session->set_flashdata('error', 'Có lỗi xảy ra.');
                }
                
                redirect('backend/news/create');
            }
        }
        
        $this->layout_title = 'Viết bài mới';
    }
    
    /**
     * Edit object
     */
    public function update() 
    {
        //Load validation lib
        $this->load->library('form_validation');
        //Load keyword library
        $this->load->library('keywords');
        //Set rules
        $this->form_validation->set_rules($this->validation_rules);
        
        //Get object_id
        $objID = $this->uri->segment(5);
        
        //Check object_id
        if(empty($objID))
        {
            redirect('/backend/news');
        }
        
        //Check is post
        if ($this->input->post())
        {
            if ($this->form_validation->run())
            {
                //Get current user
                $globals = $this->load->get_var('_globals');
                
                //Update object data
                $result = $this->blog_m->update_object($objID, array(
                        'category_id'           => $this->_categoryID,
                        'title'             => $this->input->post('title'),
                        'slug'              => $this->removeUnicode($this->input->post('title')),
                        'thumb'             => $this->input->post('thumbnail_url'),
                        'keywords'          => $this->keywords->process($this->input->post('keywords')),
                        'intro'             => $this->input->post('intro'),
                        'body'              => $this->input->post('content'),
                        'status'            => $this->input->post('status'),
                        'updated_on'		=> time(),
                        'comments_enabled'	=> $this->input->post('comment'),
                        'author_id'         => $globals['user']['id'],
                ));
                
                if ($result)
                {
                        $this->session->set_flashdata('success', sprintf('Đã chỉnh sửa bài viết "%s" thành công.', $this->input->post('title')));
                }
                else
                {
                        $this->session->set_flashdata('error', 'Có lỗi xảy ra.');
                }
                
                redirect('backend/news/update/id/'.$objID);
            }
        }
        else
        {
            //Get object detail
            $arrObjDetail = $this->blog_m->get_object_detail($objID);

            //Check array Object
            if(empty($arrObjDetail))
            {
                redirect('/backend/news');
            }
            
            //Get keyword
            if(!empty($arrObjDetail['keywords'])){
                $arrObjDetail['keywords'] = $this->keywords->get_string($arrObjDetail['keywords']);
            }
            
            //Assign to view
            $this->assign('arrObjDetail', $arrObjDetail);
            
            //Set layout title
            $this->layout_title = 'Chỉnh sửa bài viết';
        }
    }
    
    /**
     * Delete object by ojectID
     */
    public function delete()
    {
        //Get object_id
        $objID = $this->uri->segment(5);
        
        //Check object_id
        if(empty($objID))
        {
            redirect('/backend/news');
        }
        
        //Delete object
        $this->blog_m->delete_object($objID);
        
        //Return 
        redirect('/backend/news');
    }

}